package com.luxsoft.siipap.em.service;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.luxsoft.siipap.em.replica.utils.SiipapJdbcTemplateFactory;
import com.luxsoft.siipap.service.ServiceLocator2;




public final class EMServiceLocator {
	
	private static EMServiceLocator INSTANCE;
	
	
	
	private ApplicationContext context;
		
	private Logger logger=Logger.getLogger(getClass());
	
	private EMServiceLocator(){
		
	}
	
	public static EMServiceLocator instance(){
		if(INSTANCE==null){
			INSTANCE=new EMServiceLocator();
		}
		return INSTANCE;
	}
	
	public void close(){
		ClassPathXmlApplicationContext ctx=(ClassPathXmlApplicationContext)context;
		ctx.close();
	}
	
	
	
	public ApplicationContext getContext(){
		if(context==null){			
			logger.info("Inicializando contexto de servicios...");			
			ApplicationContext daoCtx=ServiceLocator2.instance().getContext();
			context=new ClassPathXmlApplicationContext(new String[]{"em-db-ctx.xml"},daoCtx);
		}
		return context;
	}
	
	public JdbcTemplate getDefaultJdbcTemplate(){
		return (JdbcTemplate)getContext().getBean("jdbcTemplate");
	}
	public SiipapJdbcTemplateFactory getSiipapTemplateFactory(){
		return (SiipapJdbcTemplateFactory)getContext().getBean("siipapJdbcTemplateFactory");
	}
	
	/**
	 * Regresa un {@link JdbcTemplate} que utiliza el un bridg JDBC-ODBC 
	 * de nombre SIIPAP
	 * 
	 * @return
	 */
	public JdbcTemplate getSiipapDBFOdbcTemplate(){
		return getSiipapTemplateFactory().getCurrentJdbcTemplate();
	}
	
	
	
	
}
